package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.common.exception.BusinessException;
import com.yike.common.pagination.PageInfo;
import com.yike.common.pagination.Paging;
import com.yike.common.tool.LoginUtil;
import com.yike.common.tool.StringUtil;
import com.yike.user.entity.*;
import com.yike.user.mapper.*;
import com.yike.user.param.MerchantPageParam;
import com.yike.user.param.MerchantParam;
import com.yike.user.service.PlatformMerchantService;
import com.yike.user.vo.PlatformMerchantVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平台用户 服务实现类
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@Service
public class PlatformMerchantServiceImpl implements PlatformMerchantService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public boolean saveMerchant(MerchantParam merchantParam) throws Exception {
        // 添加商户时需要判断当前联系人手机号是否已存在账号，如果不存在先添加账号
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getMobile, merchantParam.getMobile()));
        if (user == null) {
            // 创建一个用户
            user = new User();
            user.setMobile(merchantParam.getMobile());
            user.setUserNo(StringUtil.random(32)); // 生成用户ID
            user.setNickname(merchantParam.getContact());
            user.setCreateBy(LoginUtil.getUserId());
            userMapper.insert(user);
        }

        // 判断当前用户是否已是商家
        Merchant merchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                .eq(Merchant::getUserId, user.getId()));
        if (merchant != null) {
            throw new BusinessException("当前用户已是商家，请勿重复添加");
        }

        // 创建一条商户记录
        merchant = new Merchant();
        BeanUtils.copyProperties(merchantParam, merchant);
        merchant.setMerchantNo("86" + StringUtil.getStrNo());
        merchant.setUserId(user.getId());
        merchant.setCreateBy(LoginUtil.getUserId());
        merchantMapper.insert(merchant);

        return true;
    }

    @Override
    public boolean updateMerchant(MerchantParam merchantParam) throws Exception {
        // 获取商家信息
        Merchant merchant = merchantMapper.selectById(merchantParam.getId());
        if (merchant == null) {
            throw new BusinessException("商户信息异常");
        }

        BeanUtils.copyProperties(merchantParam, merchant);
        merchant.setUpdateBy(LoginUtil.getUserId());
        // 如果出现手机号变更则需要校验新手机号是否已存在账号
        if (!merchant.getMobile().equals(merchantParam.getMobile())) {
            User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                    .eq(User::getMobile, merchantParam.getMobile()));
            if (user == null) {
                // 创建一个用户
                user = new User();
                user.setMobile(merchantParam.getMobile());
                user.setUserNo(StringUtil.random(32)); // 生成用户ID
                user.setNickname(merchantParam.getContact());
                user.setCreateBy(LoginUtil.getUserId());
                userMapper.insert(user);
            }
            // 判断新用户是否已是商家
            Merchant checkMerchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                    .eq(Merchant::getUserId, user.getId()));
            if (checkMerchant != null) {
                throw new BusinessException("新手机号已存在账号，请勿重复添加");
            }
            merchant.setUserId(user.getId());
        }
        merchantMapper.updateById(merchant);

        return true;
    }

    @Override
    public boolean deleteMerchant(Long id) throws Exception {
        // 获取商家信息
        Merchant merchant = merchantMapper.selectById(id);
        if (merchant == null) {
            throw new BusinessException("商户信息异常");
        }
        merchantMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean batchDel(List<Long> ids) throws Exception {
        merchantMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public PlatformMerchantVo getMerchant(Long id) throws Exception {
        return merchantMapper.getPlatformMerchantVo(id);
    }

    @Override
    public Paging<PlatformMerchantVo> getMerchantPageList(MerchantPageParam merchantPageParam) throws Exception {
        Page<Merchant> page = new PageInfo<>(merchantPageParam);
        IPage<PlatformMerchantVo> iPage = merchantMapper.getPlatformMerchantList(page, merchantPageParam);
        return new Paging<PlatformMerchantVo>(iPage);
    }
}
